package flex.ascript.ctrls
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.CheckBox;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	import mx.core.IFactory;
	
	public class DataGridCheckBoxColumn extends DataGridColumn
	{
		private var _valueField:String;
		private var _dataProvider:ArrayCollection;
		private var _headerRenderer:CheckBoxHeaderRenderer;
		private var _itemRenderer:CheckBoxItemRenderer;
		public function DataGridCheckBoxColumn(columnName:String=null)
		{
			super(columnName);
			//headerRenderer = new ClassFactory(CheckBoxHeaderRenderer);
//			_itemRenderer = new ClassFactory(CheckBoxItemRenderer);
//			itemRenderer = _itemRenderer;
			
			this.sortable = false;
			this.minWidth = 30;
			this.width = 30;

			_headerRenderer = new CheckBoxHeaderRenderer();
			_headerRenderer.addEventListener(Event.CHANGE,headerChangeEventHandler);
			this._headerRenderer.toolTip = "全选";
			headerRenderer = _headerRenderer;
			
			_itemRenderer = new CheckBoxItemRenderer();
			_itemRenderer.addEventListener(Event.CHANGE,itemChangeEventHandler);
			itemRenderer = _itemRenderer;
		}
		
		/**
		 * checkbox要操作的字段名称
		 * */
		public function set valueField(value:String):void{
			this._valueField = value;
			_itemRenderer.dataField = this._valueField;
		}
		
		//设置数据源
		public function set dataProvider(value:Object):void{
			try
			{
				this._dataProvider = value as ArrayCollection;
			}
			catch(e:Error)
			{
			}
		}
		
		//标题栏checkBox选中状态时
		protected function headerChangeEventHandler(event:Event):void{
			this._headerRenderer.toolTip = this._headerRenderer.selected ? "全不选":"全选";
			var Sel:String = this._headerRenderer.selected.toString();
			if(this._dataProvider != null)
			{		
				for(var i:int=0;i<this._dataProvider.length;i++)
				{
					this._dataProvider.getItemAt(i)[_valueField]=(this._headerRenderer.selected).toString();
				}
				this._dataProvider.refresh();
			}
			//发布事件
		}
		
		//数据项选中状态改变时
		protected function itemChangeEventHandler(event :Event) : void
		{
			if(this._itemRenderer.selected==false)
			{
				this._headerRenderer.selected = false;
			}
		}
		
	}
	
	
}